#coding=utf-8
from settings import getelementini, setdriver, savedriver
from elements import getelements
from actions import actions
from selenium import webdriver
import time

test_driver = savedriver.SaveDriver()

element_dict = getelementini.getini()

getelement = getelements.getelement()

#登录用例
def login_taishan(url):

    '''
        验证taishan登录
    '''

    #webdriver初始化，传入taishan的url的xpath
    driver = setdriver.set_driver(url)

    #获取登录用户名输入框的xpath
    ele_login_username = element_dict.get_ele("taishan_login","login_username")

    #获取登录密码输入框的xpath
    ele_login_password = element_dict.get_ele("taishan_login","login_password")

    #获取登录按钮的xpath
    ele_login_button = element_dict.get_ele("taishan_login","login_button")

    #获取登录成功后用户名文本的xpath
    ele_login_success = element_dict.get_ele("taishan_login","login_success")

    #登录输入用户名
    actions.ele_sendKey(getelement.get_xpath(driver,ele_login_username),"privateTesting")

    #登录输入密码
    actions.ele_sendKey(getelement.get_xpath(driver,ele_login_password),"huimei2020")

    #点击登录按钮
    actions.ele_click(getelement.get_xpath(driver,ele_login_button))

    login_success = getelement.get_xpath_text(driver,ele_login_success)

    test_driver.save_driver(driver)

    return login_success

#验证taishan的title
def taishan_title():

    '''
        验证tanshan的title
    '''

    driver = test_driver.get_driver()

    ele_title = element_dict.get_ele("taishan_page","title")


    return getelement.get_xpath_attribute(driver,ele_title,"src")

#搜索处下拉框
def taishan_sug():

    '''
    验证搜索处的下拉框
    '''

    driver = test_driver.get_driver()

    ele_sug = element_dict.get_ele("taishan_page","sug")

    actions.ele_click(getelement.get_xpath(driver,ele_sug))

    ele_sug_ul = element_dict.get_ele("taishan_page","sug_ul")

    li_text_list = getelement.get_li_text_list(driver,ele_sug_ul)

    li_text_list1 = ["医院文献库","评估表","药品说明","诊断知识库","检验检查"]

    i = 0

    for li_text in li_text_list1:

        if li_text in li_text_list:

            i = i+1

        else:

            i = 0

    return i


#搜索功能
def taishan_search():

    #获取driver
    driver = test_driver.get_driver()

    #找到搜索输入框
    search_input = element_dict.get_ele("taishan_page","searchinput")

    #找到搜索按钮
    search_button = element_dict.get_ele("taishan_page","searchbutton")

    #获取点击搜索后的页面
    taishan_jump = element_dict.get_ele("wenxian_page","taishan_jump")

    #搜索框输入肺炎
    actions.ele_sendKey(getelement.get_xpath(driver,search_input),"肺炎")

    #点击搜索
    actions.ele_click(getelement.get_xpath(driver,search_button))

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    #获取搜索出来的第一个标题
    doc = getelement.get_xpath_text(driver,taishan_jump)

    #如果标题中包含『肺炎』这个词，返回肺炎字符串表示通过，否则为失败
    if "肺炎" in doc:

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "肺炎"
    else:

        return "fail"


#验证临床应用中的图标
def linchuang_app():
    '''
        验证临床应用中的图标
    '''

    driver = test_driver.get_driver()

    linchuang_doc = element_dict.get_ele("taishan_page","linchuang_app_doc")

    linchuang_ame = element_dict.get_ele("taishan_page","linchuang_app_ame")

    doc = getelement.get_xpath_text(driver,linchuang_doc)

    ame = getelement.get_xpath_text(driver,linchuang_ame)

    if doc == "医院文献库" and ame == "惠每诊疗路径":

        return "true"

    elif doc == "惠每文献库" and ame != "惠每诊疗路径":

        return "ame fail"

    elif doc != "惠每文献库" and ame == "惠每诊疗路径":

        return "doc fail"

    else:

        return "fail"


#验证文献库页面跳转
def doc_jump():

    '''
        验证文献库跳转
    '''

    driver = test_driver.get_driver()

    linchuang_doc = element_dict.get_ele("taishan_page","linchuang_app_doc")

    doc_search = element_dict.get_ele("wenxian_page","taishan_linchuang_doc_jump")

    actions.ele_click(getelement.get_xpath(driver,linchuang_doc))

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    doc = getelement.get_xpath_text(driver,doc_search)

    if doc == "搜索":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"


#验证ame页面跳转
def ame_jump():

    '''
        验证ame跳转
    '''

    driver = test_driver.get_driver()

    linchuang_ame = element_dict.get_ele("taishan_page","linchuang_app_ame")

    ame_title = element_dict.get_ele("ame_page","taishan_linchuang_ame_jump")

    actions.ele_click(getelement.get_xpath(driver,linchuang_ame))

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    ame = getelement.get_xpath_text(driver,ame_title)

    if ame == "诊疗路径":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"


#验证医院管理中的图标
def hospital_manage():

    driver = test_driver.get_driver()

    tesla = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_tesla"))

    fuxi = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_fuxi"))

    se = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_se"))

    knowlage = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_knowlage"))

    songshan = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_songshan"))

    lvdao = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_lvdao"))

    zhuangzhou = getelement.get_xpath_text(driver,element_dict.get_ele("taishan_page","hospital_manage_zhuangzhou"))

    manage_list = ["临床质量管理平台","大数据分析平台","综合管理平台","医院知识库","病历质控平台","专病数据平台","临床数据分析平台"]

    manage_assert_list = [tesla,fuxi,se,knowlage,songshan,lvdao,zhuangzhou]

    if manage_assert_list == manage_list:

        test_driver.save_driver(driver)

        return "true"

    else:
        for asser in manage_assert_list:

            if asser not in manage_list:

                return "fail："+asser+"未找到"



#验证tesla跳转
def tesla_jump():

    driver = test_driver.get_driver()

    tesla = getelement.get_xpath(driver,element_dict.get_ele("tesla","taishan_tesla_jump"))

    actions.ele_click(tesla)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    tesla_first_name = getelement.get_xpath_text(driver,element_dict.get_ele("tesla","first_name"))

    if tesla_first_name == "单病种统计":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"






#验证fuxi跳转
def fuxi_jump():

    driver = test_driver.get_driver()

    fuxi = getelement.get_xpath(driver,element_dict.get_ele("fuxi","taishan_fuxi_jump"))

    actions.ele_click(fuxi)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    fuxi_title = getelement.get_xpath_text(driver,element_dict.get_ele("fuxi","fuxi_title"))

    if fuxi_title == "FUXI 大数据分析平台":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail---"+fuxi_title




#验证admin_se跳转
def se_jump():

    driver = test_driver.get_driver()

    se = getelement.get_xpath(driver,element_dict.get_ele("se","taishan_se_jump"))

    actions.ele_click(se)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    se_title = getelement.get_xpath_text(driver,element_dict.get_ele("se","se_title"))

    if se_title == "综合管理平台":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail---"+se_title



#验证医院知识库跳转
def knowlage_jump():

    driver = test_driver.get_driver()

    knowlage = getelement.get_xpath(driver,element_dict.get_ele("knowlage","taishan_knowlage_jump"))

    actions.ele_click(knowlage)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    knowlage_text = getelement.get_xpath_text(driver,element_dict.get_ele("knowlage","knowlage_text"))

    if knowlage_text == "—— 让健康惠及每一个人。":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"




#验证嵩山跳转
def songshan_jump():

    driver = test_driver.get_driver()

    songshan = getelement.get_xpath(driver,element_dict.get_ele("songshan","taishan_songshan_jump"))

    actions.ele_click(songshan)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    songshan_user = getelement.get_xpath_text(driver,element_dict.get_ele("songshan","songshan_user"))

    if songshan_user == "Hi  privateTesting ":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"




#验证绿道跳转
def lvdao_jump():

    driver = test_driver.get_driver()

    lvdao = getelement.get_xpath(driver,element_dict.get_ele("lvdao","taishan_lvdao_jump"))

    actions.ele_click(lvdao)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    lvdao_text = getelement.get_xpath_text(driver,element_dict.get_ele("lvdao","lvdao_text"))

    if lvdao_text == "病历入组":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"





#验证庄周跳转
def zhuangzhou_jump():

    driver = test_driver.get_driver()

    zhuangzhou = getelement.get_xpath(driver,element_dict.get_ele("zhuangzhou","taishan_zhuangzhou_jump"))

    actions.ele_click(zhuangzhou)

    #获取所有句柄
    handle = driver.window_handles

    #焦点移至文献标签页
    driver.switch_to_window(handle[-1])

    zhuangzhou_text = getelement.get_xpath_text(driver,element_dict.get_ele("zhuangzhou","zhuangzhou_text"))

    if zhuangzhou_text == "填报汇总":

        #关闭文献标签页
        driver.close()

        #焦点移动至taishan标签页
        driver.switch_to_window(handle[0])

        #存储driver
        test_driver.save_driver(driver)

        #返回结果
        return "true"

    else:

        return "fail"












if __name__ == "__main__":

    login_taishan("http://taishan.huimei.com")

    # songshan_page_ele()